<!DOCTYPE html>
<html lang="en">
<!-- https://github.com/JailBreakC/flex-box-demo -->

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body {
            padding: 20px;
            margin: 0 auto;
            background-color: #E2E2E2;
            max-width: 1024px;
            color: #595B66;
            font-family: 'Microsoft Yahei', sans-serif;
        }

        .box {
            /* Safari Webkit内核的浏览器，必须加上-webkit前缀*/
            /* display: -webkit-flex;
            display: flex; */
            background-color: white;
            margin: 0 0 55px;
            display: flex;
        }

        .top {
            background-color: #797979;
            color: #E2E2E2;
            padding: 15px;
            border-radius: 25px;
            margin-top: 10px;
        }

        a {
            color: #FFD200;
        }

        h1,
        h2 {
            border-bottom: solid 2px #797979;
        }

        .box-1 {
            margin-top: 10px;
            flex-direction: row;
        }

        .box-item {
            width: 200px;
            height: 200px;
            background-color: #FFD200;
            margin: 5px;
            font-size: 100px;
            text-align: center;
            line-height: 200px;
            color: white;
        }

        .box-2 {
            margin-top: 10px;
            flex-direction: row;
            flex-wrap: wrap;
        }

        .box-3 {
            margin-top: 10px;
            flex-direction: row;
            flex-wrap: wrap-reverse;
        }

        .box-4 {
            margin-top: 10px;
            flex-flow: row wrap;
        }

        .box-5 {
            margin-top: 10px;
            flex-flow: row wrap;
            justify-content: flex-start;
        }

        .box-6 {
            margin-top: 10px;
            flex-flow: row wrap;
            justify-content: flex-end;
        }

        .box-7 {
            margin-top: 10px;
            flex-flow: row wrap;
            justify-content: center;
        }

        .box-8 {
            margin-top: 10px;
            flex-flow: row wrap;
            justify-content: space-between;
        }

        .box-9 {
            margin-top: 10px;
            flex-flow: row wrap;
            justify-content: space-around;
        }

        .box-10 {
            margin-top: 10px;
            align-items: flex-start;
        }

        .box-11 {
            margin-top: 10px;
            align-items: flex-end;
        }

        .box-12 {
            margin-top: 10px;
            align-items: center;
        }

        .box-13 {
            margin-top: 10px;
            align-items: baseline;
        }

        .box-14 {
            margin-top: 10px;
            align-items: stretch;
        }

        .box-14 .box-item {
            height: auto;
        }

        .box-15 {
            margin-top: 10px;
            flex-flow: row wrap;
            align-content: flex-start;
        }

        .box-16 {
            margin-top: 10px;
            flex-flow: row wrap;
            align-content: flex-end;
        }

        .box-17 {
            margin-top: 10px;
            flex-flow: row wrap;
            align-content: center;
        }

        .box-18 {
            margin-top: 10px;
            flex-flow: row wrap;
            align-content: space-between;
        }

        .box-19 {
            margin-top: 10px;
            flex-flow: row wrap;
            align-content: space-around;
        }

        .box-20 {
            margin-top: 10px;
            flex-flow: row wrap;
            align-content: stretch;
        }

        .box-20 .box-item {
            height: auto;
        }

        .box-21 .order {
            order: -1;
        }

        .box-22 .order {
            flex-grow: 2
        }

        .box-23 .box-item {
            width: 400px;
        }

        .box-23 .shrink {
            flex-shrink: 0;
        }

        .box-24 .box-item {
            flex-basis: 200px;
            /* width 将失去作用 */
            width: 400px;
        }

        .box-25 {
            height: 400px;
        }

        .box-25 .self {
            align-self: flex-end
        }

        .box-tall {
            height: 800px;
        }

        .item-tall {
            height: 400px;
            line-height: 400px;
        }
    </style>
</head>

<body>
    <h1>Flex布局实例</h1>
    <div class="top">
        <p>感谢阮一峰老师的教程
            <a target="_blank" href="http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html">http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html</a>
        </p>
        <p>本示例将教程上所有的布局都简单的实现了一遍，并保存GitHub上面</p>
        <p>
            <a target="_blank" href="https://github.com/JailBreakC/flex-box-demo">https://github.com/JailBreakC/flex-box-demo</a>
        </p>
    </div>
    <h1>容器的属性</h1>
    <h3>1、flex-direction属性</h3>
    <div class="top">
        flex-direction属性决定主轴的方向（即项目的排列方向）。
    </div>
    <div class="top">
        .box { flex-direction: row | row-reverse | column | column-reverse; }
    </div>
    <div class="box box-1">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
    </div>
    <h3>2、flex-wrap属性</h3>
    <div class="top">默认情况下，项目都排在一条线（又称"轴线"）上。flex-wrap属性定义，如果一条轴线排不下，如何换行。</div>
    <div class="top">.box{ flex-wrap: nowrap | wrap | wrap-reverse; }</div>
    <div class="top">nowrap（默认）：不换行。</div>
    <div class="box box-1">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
        <div class="box-item">10</div>
        <div class="box-item">11</div>
        <div class="box-item">12</div>
    </div>
    <div class="top">wrap：换行，第一行在上方。</div>
    <div class="box box-2">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">wrap-reverse：换行，第一行在下方。</div>
    <div class="box box-3">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <h3>3、flex-flow</h3>
    <div class="top">flex-flow属性是flex-direction属性和flex-wrap属性的简写形式，默认值为row nowrap。</div>
    <div class="top">.box { flex-flow: &lt;flex-direction&gt; || &lt;flex-wrap&gt;; }</div>
    <div class="box box-4">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <h3>4、justify-content属性</h3>
    <div class="top">justify-content属性定义了项目在主轴上的对齐方式。</div>
    <div class="top">.box { justify-content: flex-start | flex-end | center | space-between | space-around; }</div>
    <div class="top">flex-start（默认值）：左对齐</div>
    <div class="box box-5">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">flex-end：右对齐</div>
    <div class="box box-6">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">center： 居中</div>
    <div class="box box-7">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">space-between：两端对齐，项目之间的间隔都相等。</div>
    <div class="box box-8">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">space-around：每个项目两侧的间隔相等。所以，项目之间的间隔比项目与边框的间隔大一倍。</div>
    <div class="box box-9">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
    </div>
    <h3>5、align-items属性</h3>
    <div class="top">align-items属性定义项目在交叉轴上如何对齐。</div>
    <div class="top">.box { align-items: flex-start | flex-end | center | baseline | stretch; }</div>
    <div class="top">flex-start：交叉轴的起点对齐。</div>
    <div class="box box-10">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
        <div class="box-item">5</div>
        <div class="box-item item-tall">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">flex-end：交叉轴的终点对齐。</div>
    <div class="box box-11">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
        <div class="box-item">5</div>
        <div class="box-item item-tall">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">center：交叉轴的中点对齐。</div>
    <div class="box box-12">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
        <div class="box-item">5</div>
        <div class="box-item item-tall">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">baseline: 项目的第一行文字的基线对齐。</div>
    <div class="box box-13">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
        <div class="box-item">5</div>
        <div class="box-item item-tall">6</div>
        <div class="box-item">7</div>
    </div>
    <div class="top">stretch（默认值）：如果项目未设置高度或设为auto，将占满整个容器的高度。</div>
    <div class="box box-14">
        <div class="box-item">1</div>
        <div class="box-item item-tall">2</div>
        <div class="box-item">3</div>
        <div class="box-item item-tall">4</div>
        <div class="box-item">5</div>
        <div class="box-item item-tall">6</div>
        <div class="box-item">7</div>
    </div>
    <h3>6、align-content属性</h3>
    <div class="top">align-content属性定义了多根轴线（多行）的对齐方式。如果项目只有一根轴线，该属性不起作用。</div>
    <div class="top">.box { align-content: flex-start | flex-end | center | space-between | space-around | stretch; }</div>
    <div class="top">flex-start：交叉轴的起点对齐。</div>
    <div class="box box-tall box-15">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
    </div>
    <div class="top">flex-end：与交叉轴的终点对齐。</div>
    <div class="box box-tall box-16">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
    </div>
    <div class="top">center：与交叉轴的中点对齐。</div>
    <div class="box box-tall box-17">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
    </div>
    <div class="top">space-between：与交叉轴两端对齐，轴线之间的间隔平均分布。</div>
    <div class="box box-tall box-18">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
    </div>
    <div class="top">space-around：每根轴线两侧的间隔都相等。所以，轴线之间的间隔比轴线与边框的间隔大一倍。</div>
    <div class="box box-tall box-19">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
    </div>
    <div class="top">stretch（默认值）：轴线占满整个交叉轴。不能固定高度，要设置auto</div>
    <div class="box box-tall box-20">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item">4</div>
        <div class="box-item">5</div>
        <div class="box-item">6</div>
        <div class="box-item">7</div>
        <div class="box-item">8</div>
        <div class="box-item">9</div>
    </div>

    <h1>项目的属性</h1>
    <div class="top">1、order属性</div>
    <div class="top">order属性定义项目的排列顺序。数值越小，排列越靠前，默认为0。</div>
    <div class="top"> .item { order: &lt;integer&gt;; }</div>
    <div class="box box-21">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
        <div class="box-item order">4</div>
    </div>
    <h3>2、flex-grow属性</h3>
    <div class="top">flex-grow属性定义项目的放大比例，默认为0，即如果存在剩余空间，也不放大。</div>
    <div class="top">如果所有项目的flex-grow属性都为1，则它们将等分剩余空间（如果有的话）。如果一个项目的flex-grow属性为2，其他项目都为1，则前者占据的剩余空间将比其他项多一倍。</div>
    <div class="top"> .item { flex-grow: &lt;number&gt;; /* default 0 */ }</div>
    <div class="box box-22">
        <div class="box-item">1</div>
        <div class="box-item order">2</div>
        <div class="box-item">3</div>
    </div>
    <h3>3、flex-shrink</h3>
    <div class="top">flex-basis属性定义了在分配多余空间之前，项目占据的主轴空间（main size）。浏览器根据这个属性，计算主轴是否有多余空间。它的默认值为auto，即项目的本来大小。</div>
    <div class="top"> .item { flex-basis: &lt;length&gt;; | auto; /* default auto */ }</div>
    <div class="box box-23">
        <div class="box-item">1</div>
        <div class="box-item shrink">2</div>
        <div class="box-item">3</div>
    </div>
    <h3>4、flex-basis属性</h3>
    <div class="top">flex-basis属性定义了在分配多余空间之前，项目占据的主轴空间（main size）。浏览器根据这个属性，计算主轴是否有多余空间。它的默认值为auto，即项目的本来大小。</div>
    <div class="top"> .item { flex-basis: &lt;length&gt;; | auto; /* default auto */ }</div>
    <div class="box box-24">
        <div class="box-item">1</div>
        <div class="box-item">2</div>
        <div class="box-item">3</div>
    </div>
    <h3>5、flex属性</h3>
    <div class="top">flex属性是flex-grow, flex-shrink 和 flex-basis的简写，默认值为0 1 auto。后两个属性可选。</div>
    <div class="top">该属性有两个快捷值：auto (1 1 auto) 和 none (0 0 auto)。</div>
    <div class="top">.item { flex: none | [ &lt;'flex-grow'&gt; &lt;'flex-shrink'&gt;? || &lt;'flex-basis'&gt; ] }</div>
    <h3>6、align-self属性</h3>
    <div class="top">align-self属性允许单个项目有与其他项目不一样的对齐方式，可覆盖align-items属性。默认值为auto，表示继承父元素的align-items属性，如果没有父元素，则等同于stretch。</div>
    <div class="top">.item { align-self: auto | flex-start | flex-end | center | baseline | stretch; }</div>
    <div class="box box-25">
        <div class="box-item">1</div>
        <div class="box-item self">2</div>
        <div class="box-item">3</div>
    </div>
</body>

</html>